/*
 * Galaxium Messenger
 * 
 * Copyright (C) 2003-2007 Philippe Durand <draekz@gmail.com>
 * Copyright (C) 2007 Ben Motmans <ben.motmans@gmail.com>
 * 
 * License: GNU General Public License (GPL)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

using System;
using System.Collections.Generic;

using Gtk;
using Glade;

using Galaxium.Core;
using Galaxium.Gui;
using Galaxium.Gui.GtkGui;

namespace Galaxium.Protocol.Msn.GtkGui
{
	public sealed class CopyMoveGroupDialog
	{
		[Widget ("CopyMoveGroupDialog")]
		private Dialog _dialog = null;
		[Widget ("boxGroups")]
		private HBox _boxGroups = null;
		[Widget ("imgDialog")]
		private Image _imgDialog = null;
		[Widget ("btnApply")]
		private Button _btnApply = null;
		
		public string SelectedGroupID { get { return GetSelectedGroupID(); } }
		
		private ComboBox _comboGroups;
		private ListStore _storeGroups;
		private MsnSession _session;
		private bool _docopy;
		private bool _usable = false;
		
		public CopyMoveGroupDialog(MsnSession session, MsnContact contact, bool docopy)
		{
			_session = session;
			_docopy = docopy;
			
			XML gxml = new XML (GladeUtility.GetGladeResourceStream (typeof (CopyMoveGroupDialog).Assembly, "CopyMoveGroupDialog.glade"), null, null);
			gxml.Autoconnect (this);
			
			if (_docopy)
			{
				_dialog.Icon = IconUtility.GetIcon ("galaxium-copy", IconSizes.Small);
				_imgDialog.FromPixbuf = IconUtility.GetIcon ("galaxium-copy-contact", IconSizes.Large);
				_dialog.Title = "Copy To Group";
				_btnApply.Label = "Copy";
				_btnApply.Image = new Image(IconUtility.GetIcon ("galaxium-copy", IconSizes.Small));
			}
			else
			{
				_dialog.Icon = IconUtility.GetIcon ("galaxium-move", IconSizes.Small);
				_imgDialog.FromPixbuf = IconUtility.GetIcon ("galaxium-move-contact", IconSizes.Large);
				_dialog.Title = "Move To Group";
				_btnApply.Label = "Move";
				_btnApply.Image = new Image(IconUtility.GetIcon ("galaxium-move", IconSizes.Small));
			}
			
			CellRendererText renderer = new CellRendererText();
			
			_storeGroups = new ListStore (typeof (string), typeof (string));
			_comboGroups = new ComboBox ();
			_comboGroups.Model = _storeGroups;
			_comboGroups.PackStart(renderer, false);
			_comboGroups.SetCellDataFunc(renderer, TextCellDataFunc);
			
			_comboGroups.AddAttribute(renderer, "text", 1);
			
			_boxGroups.PackStart(_comboGroups, true, true, 0);
			
			lock (_session.Groups)
			{
				foreach (MsnGroup group in _session.Groups)
				{
					if (group.UniqueIdentifier != "0" && !group.Contains(contact))
					{
						_storeGroups.AppendValues(group.UniqueIdentifier, group.Name);
						_usable = true;
					}
				}
			}
			
			_comboGroups.Active = 0;
			
			_boxGroups.ShowAll();
			_dialog.ShowAll();
		}
		
		public int Run()
		{
			if (_usable)
				return _dialog.Run();
			else
			{
				_dialog.Hide();
				_dialog.Destroy();
				MessageDialog errordialog = new MessageDialog (null, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, "This contact is already in all groups currently available!");
				errordialog.Run ();
				errordialog.Destroy ();
				return (int)ResponseType.Cancel;
			}
		}
		
		public void Destroy()
		{
			_dialog.Hide();
			_dialog.Destroy();
		}
		
		private string GetSelectedGroupID()
		{
			TreeIter iter = TreeIter.Zero;
			string guid = "";
			
			if (_comboGroups.GetActiveIter(out iter))
				guid = (string)_storeGroups.GetValue(iter, 0);
			
			return(guid);
		}
		
		private static CellLayoutDataFunc TextDataFunction = null;
		
		private static void TextDataFunc(CellLayout cell_layout, CellRenderer cell, TreeModel tree_model, TreeIter iter)
		{
			CellRendererText text = (CellRendererText)cell;
			//text.Xpad = 3;
			text.Width = 150;
			text.Ellipsize = Pango.EllipsizeMode.End;
		}
		
		private static CellLayoutDataFunc TextCellDataFunc
		{
			get
			{
				if (TextDataFunction == null)
					TextDataFunction = new CellLayoutDataFunc(TextDataFunc);
				return(TextDataFunction);
			}
		}
	}
}